<?php
/*
	会员管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}else{
		$params['mid >']=0;
	}
	if($_RQ['fromuser']){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	if($_RQ['gei_mid']){
		$params['gei_mid']=intval($_RQ['gei_mid']);
		$return['params']['gei_mid']=$_RQ['gei_mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as $k=>&$item){
			if($item['mid']==-1){
				$item['member']=array('nickname'=>'平台');
			}else{
				$item['member']=cfc('core')->member_show($item['mid']);
			}
			$item['shmember']=cfc('core')->member_show($item['fromuser']);
			$item['getmember']=cfc('core')->member_show($item['gei_mid']);
			if(!$item['member']['id']){
				//unset($list[$k]);
				$this->delete('member',array('id'=>$item['id']));	
			}
			if(!$item['is_agent']){
				$item['level_id']=-1;
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'<red>设置推荐人，会检测上级升级条件！</red>','key'=>'mid','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'mid','title'=>'MID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'shmember','title'=>'上级信息','type'=>'member'),
			array('name'=>'getmember','title'=>'我的老板','type'=>'member'),
			array('name'=>'level_id','title'=>'是否股东','type'=>'select','options'=>'levels'),
			array('name'=>'check_time','title'=>'成为股东时间','type'=>'time'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'time'),

		),'action'=>array(
			//array('type'=>'href','op'=>'list','action'=>'agent','title'=>'查看代理地区','show'=>array('is_agent'=>1)),
			//array('type'=>'modal','op'=>'add','title'=>'设置代理'),
			array('type'=>'href','op'=>'change','title'=>'修改推荐人'),
			array('type'=>'post','op'=>'stop','title'=>'禁用','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'恢复','show'=>array('stop'=>1)),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'fromuser','title'=>'上级'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'gei_mid','title'=>'老板'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['-1']=array('id'=>'-1','title'=>'普通会员');
	$return['levels']['0']=array('id'=>'0','title'=>$this->diylevelname);
	$return['types']=array(
		'0'=>'普通会员',
		'1'=>'股东',
	);
	exi($return);
}

if($_RQ['op']=='stop'){
	$mid=intval($_RQ['mid']);
	$this->model('member')->stop_agent($mid);
	exi('禁用成功！');
}
if($_RQ['op']=='start'){
	$mid=intval($_RQ['mid']);
	$this->model('member')->start_agent($mid);
	exi('恢复成功！');
}
if($_RQ['op']=='add'){
	//选择代理地区
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['area']){
			exi('请先选择代理地区~','error');
		}
		$area_code=$this->model()->get_area_code($post['area']);
	
		$agent_id=$this->model('member')->apply_agent($post['mid'],$area_code);
		$this->model('member')->check_agent($agent_id,1);
		exi('添加股东成功！','','list');
	}
	$mid=intval($_RQ['mid']);
	if(!$mid){
		exi('请先选择会员！','error');
	}
	$item=array('mid'=>$mid,'area'=>array('city'=>''));
	$return['item']=$item;
	$return['areas']=cfc('tools')->pcd();
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'添加股东',
		'edit'=>array(
			array('type'=>'pcd','name'=>'area','title'=>'选择代理地区','options'=>'areas'),
		),
	);
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$fromuser=intval($post['fromuser']);
		$this->model('member')->set_parent($mid,$fromuser,true);
		exi('修改推荐人成功！','','list');
	}
	$item=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>intval($_RQ['mid'])),array('id','mid','merchid','fromuser'));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改推荐人',
		'edit'=>array(
			array('type'=>'choose','name'=>'fromuser','title'=>'设置推荐人','action'=>'core/member.get_member/','help'=>'填写0表示设置为总店'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){

	$mid=intval($_RQ['mid']);
	if($mid){
		$this->delete('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $mid){
			$mid=intval($mid);
			if($mid){
				$this->delete('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
			}
		}
	}
	exi('删除成功！');
}
?>